package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public Role findById(String id) {
        return  roleDao.findById(id);
    }

    @Override
    public void update(Role role) {
         roleDao.updateById(role);
    }

    @Override
    public void delete(String id) {
        roleDao.deleteById(id);
    }



    @Override
    public PageInfo<Role> findPage(String companyId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo<Role>(list,5);
    }

    @Override
    public void updateRoleModule(String roleid, String moduleIds) { ////2,201,202
//        先删除这个角色下之前的权限数据  delete from pe_role_module where role_id=#{roleid}
        roleDao.deleteRoleAndModleByRoleId(roleid);
        if(StringUtils.isNotEmpty(moduleIds)) {
//        给角色分配模块菜单权限
            String[] mIds = moduleIds.split(",");
            for (String mId : mIds) {
//            insert into  pe_role_module (role_id,module_id) values (#{roleid},#{moduleid})
                roleDao.saveRoleAndModule(roleid, mId);
            }
        }
    }

    @Override
    public List<String> findModuleIdsByRoleId(String roleid) {
        return roleDao.findModuleIdsByRoleId(roleid);
    }
}
